//
//  HTDiscoverTableViewController.swift
//  weibo
//
//  Created by JonsonLiu on 2017/2/22.
//  Copyright © 2017年 JonsonLiu. All rights reserved.
//

import UIKit
import MJRefresh
private let origCellID = "dd"
private let retweetCellID = "ss"
class HTDiscoverTableViewController: HTBasController {
    
    // 顶部刷新
    let header = MJRefreshNormalHeader()
    // 底部刷新
    let footer = MJRefreshAutoNormalFooter()
    
    
    var statemodel : StatesListViewModel = StatesListViewModel()
    
    var refreshcontrol : UIRefreshControl?

    func loadData(){
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        

        
        
       statemodel.loadData(isPullUP: true) { (isSuccess) in
        if isSuccess {
            self.tableView.reloadData()
        }else{
        
            print("请求错误")
        }
        }
//       NetworkTools.shardTools.statesList {
//            print("打印列表")
//        }

        refreshcontrol = UIRefreshControl()
//        tableView.addSubview(refreshcontrol!)
        
//       refreshcontrol?.addTarget(self, action: #selector(self.loadData), for: .valueChanged)
        // 下拉刷新
        header.setRefreshingTarget(self, refreshingAction: Selector("headerRefresh"))
        // 现在的版本要用mj_header
        self.tableView.mj_header = header
        
        // 上拉刷新
        footer.setRefreshingTarget(self, refreshingAction: Selector("footerRefresh"))
        self.tableView.mj_footer = footer

        
       
        let searchView = HTSearchView.searchView()
        
        searchView.frame.size.width = UIScreen.main.bounds.width
        
        
        navigationItem.titleView = searchView
        
        
        tableView.register(UINib(nibName: "HTWeComeView", bundle: nil), forCellReuseIdentifier: origCellID)
        tableView.register(UINib(nibName: "HTWeComeViewReentw", bundle: nil), forCellReuseIdentifier: retweetCellID)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 300
        tableView.separatorStyle = .none
        
    }
    
    // 顶部刷新
    func headerRefresh(){
        print("下拉刷新")
        // 结束刷新
        statemodel.loadData(isPullUP: true) { (isSuccess) in
            if isSuccess {
                self.tableView.reloadData()
            }else{
                
                print("请求错误")
            }
        }
        self.tableView.mj_header.endRefreshing()
        
    }
    
    // 底部刷新
    var index = 0
    func footerRefresh(){
        print("上拉刷新")
        
        self.tableView.mj_footer.endRefreshing()
        // 2次后模拟没有更多数据
        index = index + 1
        if index > 2 {
            footer.endRefreshingWithNoMoreData()
        }
    }

   }


extension HTDiscoverTableViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return   statemodel.state?.count ?? 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let vm = statemodel.state?[indexPath.row]
        
        var cellID : String?
        if vm?.states?.retweeted_status != nil {
            cellID =  origCellID
        }else{
            cellID = retweetCellID
        }
        let  cell = tableView.dequeueReusableCell(withIdentifier: cellID!, for: indexPath) as! HTWeComeView
//        cell.stateusLable.text = statemodel.state?[indexPath.row].states?.text
        cell.stateModel = vm
        
        return cell
    }

}
